$(function () {
    var ctx=$("#ctxVal").val();
    var loginForm = $("#loginForm");
    $("#btn_submit").click(function () {
        loginForm.bootstrapValidator('validate');
        var flag =  loginForm.data('bootstrapValidator').isValid();
        if(flag){
            var formData = $("#loginForm").serialize();
            $.ajax({
                type: 'post',
                url:ctx+"/login.action?method=loginVerify",
                dataType: 'json',
                data:formData,
                success: function(data){
                    if(data.msg =='success'){
                        layer.msg("登录成功！");
                        window.location.href = ctx+"/index.action";
                    }else if(data.msg == 'delete'){
                        $("#password").val("");
                        layer.alert("该用户已经被冻结，请联系管理员！",{icon:5});
                    }else if(data.msg == 'errorPass'){
                        $("#password").val("");
                        layer.msg("密码错误,请重新输入",
                            {
                                time:1000*2,
                                icon:5
                            }
                        );
                    }else if(data.msg == 'null'){
                        $("#password").val("");
                        layer.msg("未查到该用户，请核证后登录！",{
                            time:1000*2,
                            icon:5
                        });
                    }
                },
                error: function(xhr, type){
                }
            });
        }
    });
    $("#btn_resetting").click(function () {
        $("#username").val("");
        $("#password").val("");
    });

    // 导航显示
    $(window).scroll(function () {
        if ($(".navbar").offset().top > 50) {$(".navbar-fixed-top").addClass("top-nav");
        }else {$(".navbar-fixed-top").removeClass("top-nav");}
    });
    loginForm.bootstrapValidator({
        message: '验证失败',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            username: {
                message: '用户名验证失败',
                validators: {
                    notEmpty: {
                        message: '用户名不能为空'
                    },
                }
            },
            password: {
                message: '密码验证失败',
                validators: {
                    notEmpty: {
                        message: '密码不能为空！'
                    },
                }
            },
        },
    });

});
